package leetcode;

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) {
        if (val < 0 || val > 9){
            throw new RuntimeException("链表节点值不能小于0, 也不能大于9");
        }
        this.val = val;
        this.next = next;
    }

    public int length(){
        int length = 0;
        ListNode node = this;
        while (node != null){
            length++;
            node = node.next;
        }
        return length;
    }

    public String toString(){
        StringBuilder sb = new StringBuilder();
        ListNode node = this;
        while (node != null){
            sb.append(node.val);
            node = node.next;
        }
        return sb.toString();
    }
}
